package org.dey.serviceImpl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dey.utils.DateUtils;
import org.dey.mapper.MemberReportMapper;
import org.dey.pojo.Member;
import org.dey.service.MemberReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author deyran
 * @classname
 * @see ServiceImpl
 */
@Service
@Transactional
public class MemberReportServiceImpl extends ServiceImpl<MemberReportMapper, Member> implements MemberReportService {

    @Autowired
    MemberReportMapper memberReportMapper;

    @Override
    //根据月份统计会员数量
    public List<Integer> getMemberCountByMonth(List<String> month) {
        List<Integer> list = new ArrayList<>();
        for (String m : month) {
            m = m + ".31";//格式：2019.04.31
            Integer count = memberReportMapper.getMemberCountByMonth(m);
            list.add(count);
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> findSetmealCount() {
        return memberReportMapper.findSetmealCount();
    }


    @Override
    public Map<String, Object> getBusinessReport() throws Exception {
        //获得当前日期
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        //获得本周一的日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //获得本月第一天的日期
        String firstDay4ThisMonth =DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
        //今日新增会员数
        Integer todayNewMember = memberReportMapper.findMemberCountByDate(today);
        //总会员数
        Integer totalMember = memberReportMapper.findMemberTotalCount();
        //本周新增会员数
        Integer thisWeekNewMember = memberReportMapper.findMemberCountAfterDate(thisWeekMonday);
        //本月新增会员数
        Integer thisMonthNewMember = memberReportMapper.findMemberCountAfterDate(firstDay4ThisMonth);
        //今日预约数
        Integer todayOrderNumber = memberReportMapper.findOrderCountByDate(today);
        //本周预约数
        Integer thisWeekOrderNumber = memberReportMapper.findOrderCountAfterDate(thisWeekMonday);
        //本月预约数
        Integer thisMonthOrderNumber = memberReportMapper.findOrderCountAfterDate(firstDay4ThisMonth);
        //今日到诊数
        Integer todayVisitsNumber = memberReportMapper.findVisitsCountByDate(today);
        //本周到诊数
        Integer thisWeekVisitsNumber = memberReportMapper.findVisitsCountAfterDate(thisWeekMonday);
        //本月到诊数
        Integer thisMonthVisitsNumber = memberReportMapper.findVisitsCountAfterDate(firstDay4ThisMonth);
        //热门套餐（取前4）
        List<Map> hotSetmeal = memberReportMapper.findHotSetmeal();
        Map<String, Object> result = new HashMap<>();
        result.put("reportDate", today);
        result.put("todayNewMember", todayNewMember);
        result.put("totalMember", totalMember);
        result.put("thisWeekNewMember", thisWeekNewMember);
        result.put("thisMonthNewMember", thisMonthNewMember);
        result.put("todayOrderNumber", todayOrderNumber);
        result.put("thisWeekOrderNumber", thisWeekOrderNumber);
        result.put("thisMonthOrderNumber", thisMonthOrderNumber);
        result.put("todayVisitsNumber", todayVisitsNumber);
        result.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
        result.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
        result.put("hotSetmeal", hotSetmeal);
        return result;
    }

}
